var rnid;
var time; // 修改之前的时间

$(function() {
	rnid = rnidx;
	time = timex;
	var obj = 'pcid';
	var communityUrl = '/CMS/select/getCompanyList.ht';
	initCompanySelect2(obj, communityUrl, null, "communityName");
	searchCommunity(null);
	$("#pcid").change(function() {
		disabledFalse("cid");
		$("#cid").empty();
		searchCommunity($(this).val());
		$("#eid").val("");
		$("#employeeName").val("");
	});
	$("#cid").change(function() {
		$("#eid").val("");
		$("#employeeName").val("");
	});
	$("#cid").select2({
		placeholder : "请选择小区"
	});

	$("#eid").val(employeeIds);
	$("#employeeName").val(employeeNames);
});

function saveOrUpdate() {
	if (chkNull("pcid", "请选择公司")) {
		return;
	}
	if (chkNull("noticeTime", "请输入通知时间")) {
		return;
	}
	var reg = /^([1-9]\d*)$/;
	if (!reg.test($("#noticeTime").val())) {
		_alert("通知时间请输入整数");
		return;
	}
	if (chkNull("eid", "请选择通知人")) {
		return;
	}

	var rnid = $("#rnid").val();

	if (rnid) { // 修改
		var msg = "确认修改嘛？";
		_confirm("<span style='color:#FE6600'>" + msg + "</span>", function() {
			submit('/CMS/dmsrnEdit/edit.ht');
		});
	} else { // 添加
		var msg = "确认添加嘛？";
		_confirm("<span style='color:#FE6600'>" + msg + "</span>", function() {
			submit('/CMS/dmsrnEdit/save.ht');
		});
	}
}

function submit(url) {
	showProccessBar();

	var pcid = $("#pcid").val();
	if (!pcid)
		pcid = $("#pcidSelect").val();
	var cid = $("#cid").val();
	if (!cid)
		cid = $("#cidSelect").val();
	var noticeTime = $("#noticeTime").val();

	var eid = $("#eid").val();
	var employeeNames = $("#employeeName").val();
	var employeeNameArr = employeeNames.split(",");
	var size = employeeNameArr.length;
	var arr = new Array();
	for (var i = 0; i < size; i++) {
		var employeeName = employeeNameArr[i];
		arr.push(employeeName.split("(")[0])
	}
	var employeeName = arr.join(",");
	var rnid = $("#rnid").val();
	var web_token = $("input[name='web_token']").val();
	$.ajax({
		url : url,
		type : "POST",
		data : {
			rnid : rnid,
			pcid : pcid,
			cid : cid,
			noticeTime : noticeTime,
			time : time,
			eid : eid,
			employeeName : employeeName,
			web_token : web_token
		},
		error : hideProccessBar,
		success : function(data) {
			if (data.code == 1) {
				_alert(data.message);
				hideProccessBar();
			} else {
				var url = '/CMS/dmsrn/list.ht';
				$.go(url, {
					dataType : 'html',
					error : function() {
						hideProccessBar();
					},
					success : function(data, textStatus) {
						replaceDocument(url, data);
					}
				});
			}
		}
	});
}

function searchCommunity(pcid) {
	var id = "cid";
	var url = "/CMS/select/getCommunityList.ht";
	if (pcid) {
		url += "?pcid=" + pcid;
	}
	initCommunitySelect2(id, url, null, false, "communityName")
}

function returnBack() {
	var url = '/CMS/dmsrn/list.ht';
	$.go(url, {
		dataType : 'html',
		beforeSend : function() {
			showProccessBar();
		},
		error : function() {
			hideProccessBar();
		},
		success : function(data, textStatus) {
			replaceDocument(url, data);
		}
	});
}

/**
 * 获取员工列表
 */
function getEmployeeList() {
	var targetHtml = "";
	if (employeeIds != null && employeeIds != '') {
		var employeeIdArray = employeeIds.split(",");
		var employeeNameArray = employeeNames.split(",");
		for (var i = 0; i < employeeIdArray.length; i++) {
			targetHtml = targetHtml + "<li id=" + employeeIdArray[i] + ">" + employeeNameArray[i] + "</li>";
		}
	}
	var url = '/CMS/employeeAccessControl/getEmployeeList.ht';
	getList(url, "employee", "员工列表", "员工名", targetHtml);
}

/**
 * 保存列表
 */
function saveTemplate() {
	var eleId = $("#orgTypeHidden").val();
	var idArr = new Array();
	var nameArr = new Array();
	$.each($("#target").find("li"), function(index, item) {
		idArr.push($(item).attr("id"));
		nameArr.push($(item).text());
	});
	var idStr = idArr.join();
	var nameStr = nameArr.join();
	nameStr = nameStr.replace(/\([^\)]*\)/g, "");
	$("#" + eleId + "Id").val(idStr);
	$("#" + eleId + "Name").val(nameStr);
	$("#eid").val(idStr);
	$("#employeeName").val(nameStr);
	employeeIds = idStr;
	employeeNames = nameStr;
	closeDialogLock();
}

/**
 * 获取列表
 */
function getList(url, id, title, placeholder, targetHtml) {
	var targetIds = new Array();

	if (employeeIds != null && employeeIds != '') {
		var employeeIdArray = employeeIds.split(",");
		for (var i = 0; i < employeeIdArray.length; i++) {
			targetIds.push(employeeIdArray[i]);
		}
	}

	var search = $("#searchTx").val();

	var pcid = $("#pcid").val();
	if (rnid)
		pcid = $("#pcidSelect").val();
	if (pcid == null) {
		_alert("请选择公司");
		return;
	}
	var cid = $("#cid").val();
	if (rnid) {
		cid = $("#cidSelect").val();
	}
	if (!cid) {
		cid = "";
	}

	var data = {
		"villageId" : cid,
		"companyId" : pcid,
		"search" : search
	};
	$.each($("#target").find("li"), function(index, item) {
		targetIds.push($(item).attr("id"));

	});
	$.ajax({
		url : url,
		type : "post",
		dataType : "json",
		data : data,
		beforeSend : showProccessBar,
		error : hideProccessBar,
		success : function(value) {
			if (value.code == 0) {
				var arr = value.result;
				var ele = "";

				for (var i = 0; i < arr.length; i++) {
					if (targetIds.indexOf(arr[i].id) == -1) {
						ele = ele + "<li id=" + arr[i].id + ">" + arr[i].name + "</li>";
					}
				}
				$("#origin").html(ele);
				$("#target").html(targetHtml);
				$("#middle").html('<a href="javascript:void(0)" id="right">选中</a><a href="javascript:void(0)" id="left">取消</a>');
				initBox();
				$("#boxTitle").text(title);
				$("#orgTypeHidden").val(id);
				$("#searchTx").attr("placeholder", placeholder)
				$("#show-box").css("display", "block");
			} else {
				_alert(value.message);
			}
			hideProccessBar();
		}
	});
}

/**
 * 初始化选择盒
 */
function initBox() {
	$(".box").orso({
		boxl : ".box_l",
		// 左边大盒子
		boxr : ".box_r",
		// 右边大盒子
		boxlrX : "li",
		// 移动小盒子
		boxon : "on",
		// 点击添加属性
		idclass : true,
		// 添加的属性是否为class//true=class; false=id;
		boxlan : "#left",
		// 单个向左移动按钮
		boxran : "#right",
		// 单个向右移动按钮
		boxtan : "#top",
		// 单个向上移动按钮
		boxban : "#bottom",
		// 单个向下移动按钮
		boxalllan : "#allleft",
		// 批量向左移动按钮
		boxallran : "#allright",
		// 批量向右移动按钮
		boxalltan : "#alltop",
		// 移动第一个按钮
		boxallban : "#allbottom" // 移动最后一个按钮
	});
};

/**
 * 搜索
 */
function searchList() {
	var targetHtml = $("#target").html();
	var url = '/CMS/employeeAccessControl/getEmployeeList.ht';
	getList(url, "employee", "员工列表", "员工名", targetHtml);

}

/**
 * 关闭弹出窗
 */
function closeDialogLock() {
	$("#show-box").css("display", "none");
}